﻿
IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CF_AddUpdateCatalog_V2]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_CF_AddUpdateCatalog_V2];
GO

CREATE PROCEDURE [dbo].[sproc_CF_AddUpdateCatalog_V2]
    @catalog    nvarchar(255),
    @creator    nvarchar(50),
    @createNew  bit = 1,
    @catalogId  int output
AS
BEGIN
SET NOCOUNT ON;

IF ( @createNew = 1 )
BEGIN
    -- create a new catalog
    INSERT INTO dbo.UDS_CF_Catalog (Catalog, Creator )
        VALUES( @catalog, @creator );
    SET @catalogId = @@IDENTITY;
END
ELSE
BEGIN
    UPDATE dbo.UDS_CF_Catalog
        SET
            Catalog = @catalog,
            Creator = @creator
        WHERE CatalogID = @catalogId;

    IF ( @@ROWCOUNT = 0 )
    BEGIN
        RAISERROR(N'Failed to update the specified catalog "%d" since it does not exist.', 16, 1, @catalogId);
    END
END

END
GO
